package com.dfec.water.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.component.uid.core.service.UidService;
import com.dfec.framework.auth.custom.utils.CustomUserUtil;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.module.system.dao.SysUserDao;
import com.dfec.module.system.entity.SysUserEntity;
import com.dfec.water.dao.YdbApplyInfoDao;
import com.dfec.water.entity.YdbApplyInfoEntity;
import com.dfec.water.service.YdbApplyInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author df
 * @title: YdbApplyInfoServiceImpl
 * @description: 淤地坝事件上报信息Service业务层处理
 * @date Thu Jan 09 11:17:02 CST 2025
 */
@Service("ydbApplyInfoService")
@RequiredArgsConstructor
public class YdbApplyInfoServiceImpl extends ServiceImpl<YdbApplyInfoDao, YdbApplyInfoEntity> implements YdbApplyInfoService {

    private final YdbApplyInfoDao ydbApplyInfoDao;

    private final UidService uidService;

    private final SysUserDao sysUserDao;

    /**
     * 分页查询
     *
     * @param pageParam 入参
     * @return PageUtils
     * @author df
     * @date Thu Jan 09 11:17:02 CST 2025
     */
    @Override
    public PageUtils queryPage(PageParam<YdbApplyInfoEntity> pageParam) {
        PageUtils pageUtils = ydbApplyInfoDao.selectPage(pageParam);
        List<YdbApplyInfoEntity> list = (List<YdbApplyInfoEntity>) pageUtils.getList();
        if (!list.isEmpty()) {
            List<SysUserEntity> sysUserEntities = sysUserDao.selectList();
            Map<String, String> collect = sysUserEntities.stream().collect(Collectors.toMap(SysUserEntity::getUserId, SysUserEntity::getRealName));
            list.forEach(item -> {
                if (item.getApplyUserId() != null) {
                    item.setApplyUserName(collect.get(item.getApplyUserId().toString()));
                }
                if (item.getAuthUserId() != null) {
                    item.setAuthUserName(collect.get(item.getAuthUserId().toString()));
                }

            });
            pageUtils.setList(list);
        }


        return pageUtils;
    }

    /**
     * 不带分页查询
     *
     * @param ydbApplyInfoEntity 入参
     * @return List<YdbApplyInfoEntity>
     * @author df
     * @date Thu Jan 09 11:17:02 CST 2025
     */
    @Override
    public List<YdbApplyInfoEntity> listAll(YdbApplyInfoEntity ydbApplyInfoEntity) {
        return ydbApplyInfoDao.selectAll(ydbApplyInfoEntity);
    }

    @Override
    public boolean save(YdbApplyInfoEntity entity) {
        entity.setId(uidService.next());
        entity.setStatus("0");
        entity.setApplyTime(new Date());
        String userid = CustomUserUtil.getLoginUser().getUserid();
        entity.setApplyUserId(Long.parseLong(userid));
        return super.save(entity);
    }

    @Override
    public boolean updateById(YdbApplyInfoEntity entity) {
        YdbApplyInfoEntity ydbApplyInfoEntity = this.ydbApplyInfoDao.selectById(entity);
        if (StringUtils.isNotBlank(entity.getStatus()) && !ydbApplyInfoEntity.getStatus().equals(entity.getStatus())) {
            //状态不一样，则表示进行了审核
            entity.setAuthTime(new Date());
            String userid = CustomUserUtil.getLoginUser().getUserid();
            entity.setAuthUserId(Long.parseLong(userid));
        }
        return super.updateById(entity);
    }


    @Override
    public Map<String, Object> statDealData() {

        Map<String, Object> outDto = new HashMap<>(4);
        List<YdbApplyInfoEntity> ydbApplyInfoEntities = this.ydbApplyInfoDao.selectList();

        Map<String, List<YdbApplyInfoEntity>> collect = ydbApplyInfoEntities.stream().collect(Collectors.groupingBy(YdbApplyInfoEntity::getStatus));
        int totalNum = ydbApplyInfoEntities.size();
        //总任务数
        outDto.put("totalNum", totalNum);
        //待处理
        int pendingNum = collect.getOrDefault("0", new ArrayList<>()).size();
        outDto.put("pendingNum", pendingNum);
        //已处理
        int processedNum = collect.getOrDefault("1", new ArrayList<>()).size();
        outDto.put("processedNum", processedNum);
        //解决率
        double resolutionRatNum = totalNum == 0 ? 0 : ((double) processedNum / totalNum) * 100;
        outDto.put("resolutionRatNum",  (int)resolutionRatNum);

        return outDto;
    }


}